package cn.marinda.entity;

import lombok.Data;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * 服务器进程
 *
 * @author admin
 * @date 2022/07/11
 */
@Data
public class ServerProcess {
    public static ServerProcess instance = new ServerProcess();
    private InputStream errorInputStream;
    private Process process;
    /**
     * 服务器进程
     */
    private ServerProcess(){

    }

    /**
     * 启动服务器
     */
//    public void startServer() {
//        try {
//            process = Runtime.getRuntime().exec("cmd /c java -Xms1024M -Xmx3096M -jar spigot-1.12.2.jar");
//            errorInputStream = process.getErrorStream();
//            Thread.sleep(1000);
//            return;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }


    /**
     * 启动服务器
     */
    public void startServer() {
        try {
            process = Runtime.getRuntime().exec("cmd /c start.bat",null,new File("."+Server.SERVER_DIR.getPath()));
            errorInputStream = process.getErrorStream();
            Thread.sleep(1000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 启动服务器 携带参数
     */
    public void startBatServer(String batName,Server server) {
        try {
            process = Runtime.getRuntime().exec("cmd /c "+batName,null,new File("."+server.getPath()));
//            process = Runtime.getRuntime().exec("cmd /c start ." + server.getPath() + "\\"+ batName);
            errorInputStream = process.getErrorStream();
            Thread.sleep(1000);
            return;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 启动服务器 携带参数
     */
    public void startBatServerRunnable(String batName,Server server) {
        try {
            process = Runtime.getRuntime().exec("cmd /c "+batName,null,new File(server.getPath()));
//            process = Runtime.getRuntime().exec("cmd /c start ." + server.getPath() + "\\"+ batName);
            errorInputStream = process.getErrorStream();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 停止服务器
     */
    public void stopServer(){
        try {
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(process.getOutputStream());
            bufferedOutputStream.write("stop".getBytes(StandardCharsets.UTF_8));
            bufferedOutputStream.write("\n".getBytes(StandardCharsets.UTF_8));
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            process.waitFor();
            process.destroy();
            setProcess(null);
            Thread.sleep(1500);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 停止bat服务器
     */
    public void stopBatServer(){
        try {
            process.waitFor();
            process.destroy();
            setProcess(null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 重启服务器
     */
    public boolean resetServer(){
        stopServer();
        if(process == null){
            startServer();
            return true;
        }
        return false;
    }

    /**
     * 向服务器发送命令
     *
     * @return boolean
     */
    public boolean sendCommandToServer(String command){
        try {
//        输出流
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(process.getOutputStream());
            bufferedOutputStream.write(command.getBytes(StandardCharsets.UTF_8));
            bufferedOutputStream.write("\n".getBytes(StandardCharsets.UTF_8));
            bufferedOutputStream.flush();
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
